# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import models
from django.utils import timezone

class Minion(models.Model):
    KEY_TYPE = (
        (0, 'accepted'),
        (1, 'unaccepted'),
        (2, 'denied'),
        (3, 'rejected')
    )
    id = models.AutoField(primary_key = True)
    key = models.CharField(unique = True, max_length = 100,  null = False)  ## minion_id
    type = models.IntegerField(choices = KEY_TYPE, null = False)    ## key类型, 已接受, 未接受, 已拒绝, 已禁止
    status = models.BooleanField(default = False)                    ## minion节点健康状态(只有已接受的才有这个值)
    create_date = models.DateTimeField(default = timezone.now)
    update_date = models.DateTimeField(default = timezone.now)

    def __unicode__(self):
        return self.key

    def parse(self):
        ## 注意获取choice类型字段值的方法
        data = {
            'id': self.id,
            'minionId': self.key,
            'status': self.status,
            'type': self.get_type_display(),
            'createDate': self.create_date,
            'updateDate': self.update_date
        }
        minionDetail = MinionDetail.objects.get(minion = self)
        data['hwareSN'] = minionDetail.hware_sn
        data['IP'] = minionDetail.ipv4
        return data

class MinionDetail(models.Model):
    id = models.AutoField(primary_key = True)
    
    ## minion字段是Minion表的主键字段, 在数据库这个字段名为minion_id
    minion = models.OneToOneField(Minion)
    hware_sn = models.CharField(max_length = 50)      ## 硬件序列号
    kernel = models.CharField(max_length = 50)      ## 内核版本
    cpu = models.IntegerField()                  ## cpu核数
    memory = models.IntegerField()                  ## 内存容量
    os = models.CharField(max_length = 20)      ## 系统名称(centos, redhat)
    osrelease = models.CharField(max_length = 20)      ## 系统版本(7.2.1511)
    pyversion = models.CharField(max_length = 20)   ## python版本(2.7.5)
    saltpath = models.CharField(max_length = 100)      ## salt路径
    saltversion = models.CharField(max_length = 20)      ## salt版本
    osarch = models.CharField(max_length = 20)      ## 系统架构(x86_64)
    cpuarch = models.CharField(max_length = 20)      ## cpu架构(x86_64)
    cpu_model = models.CharField(max_length = 50)      ## 内核版本
    virtual = models.CharField(max_length = 20)     ## 虚拟化技术(vmware, kvm)
    mac = models.CharField(max_length = 200)        ## mac地址(可能不只一个)
    ipv4 = models.CharField(max_length = 100)       ## ip地址
    disk = models.CharField(max_length = 50)       ## 硬盘分区

    def parse(self):
        return {
            'id': self.id,    
            'minionId': self.minion.key,
            'hwareId': self.hware_sn,
            'kernel': self.kernel,
            'cpu':  self.cpu,
            'memory': self.memory,
            'os': self.os,
            'osrelease': self.osrelease,
            'pyversion': self.pyversion,
            'saltpath': self.saltpath,
            'saltversion': self.saltversion,
            'osarch': self.osarch,
            'cpuarch': self.cpuarch,
            'cpuModel': self.cpu_model,
            'virtual': self.virtual,
            'mac': self.mac,
            'ipv4': self.ipv4,
            'disk': self.disk,
        }

class Process(models.Model):
    id = models.AutoField(primary_key = True)    
    minion = models.ForeignKey(Minion)
    pid = models.IntegerField()                                 ## 进程pid
    key = models.CharField(max_length = 20)                     ## 匹配关键字
    ps_str = models.CharField(max_length = 100)                 ## 
    user = models.CharField(max_length = 50)                    ## 启动用户
    start_date = models.DateTimeField()                         ## 启动时间
    port = models.IntegerField()                                ## 占用端口
    def parse(self):
        return {
            'id': self.id,    
            'minionId': self.minion.key,
            'pid': self.pid,
            'key': self.key,
            'psStr': self.ps_str,
            'user': self.user,
            'startDate': self.start_date,
            'port': self.port,
        }

class Project(models.Model):
    id = models.AutoField(primary_key = True)    
    minion = models.ForeignKey(Minion)
    name = models.CharField(max_length = 50)  ## 进程名称, 正式命名
    owner = models.CharField(null = False, max_length = 50, default = 'root')  ## 属主
    ports = models.IntegerField(null = True)                     ## 占用端口
    path = models.CharField(max_length = 300)                   ## 进程路径
    start_cmd = models.CharField(null = False, max_length = 300)## 启动命令
    ps_str = models.CharField(max_length = 100)                 ## 
    remark = models.TextField(null = True)                        ## 备注
    def parse(self):
        return {
            'id': self.id,    
            'minionId': self.minion.key,
            'name': self.name,
            'owner': self.owner,
            'psStr': self.ps_str,
            'ports': self.ports,
            'path': self.path,
            'remark': self.remark,
        }

class SysProc(models.Model):
    '''
    系统进程表
    '''
    id = models.AutoField(primary_key = True)
    
    name = models.CharField(null = True, max_length = 50)
    ps_str = models.CharField(null = False, max_length = 300)
    desc = models.TextField(null = True)

    def parse(self):
        return {
            'id': self.id,    
            'name': self.name,
            'psStr': self.ps_str,
            'desc': self.desc
        }

class Job(models.Model):
    '''
    job任务表, 不兼容returner与job cache
    '''

    STATUS_TYPE = (
        (1, 'success'),
        (2, 'failed')
    )
    id = models.AutoField(primary_key = True)
    
    jid = models.CharField(max_length = 255, null = False)          ## 任务id, 由saltstack决定
    minion_id = models.ForeignKey(Minion)                           ## 节点id
    cmd = models.CharField(max_length = 300, null = False)          ## 执行的命令
    target = models.CharField(max_length = 300, null = False)
    status = models.IntegerField(default = 1)
    result = models.TextField(null = False)
    date = models.DateTimeField(default = timezone.now)

    def parse(self):
        return {
            'id': self.id,    
            'jid': self.jid,
            'minionId': self.minion_id.key,
            'cmd': self.cmd,
            'target': self.target,
            'status': self.status,
            'date': self.date,
            'result': self.result
        }

## class Server(models.Model):
##     '''
##     cmdb管理系统的服务器记录, 用于比对
##     '''
##     hware_sn = models.CharField(unique = True, max_length = 50)                      ## 硬件序列号
##     ips = models.CharField(max_length = 300, null = True)                            ## IP
##     bussiness = models.CharField(max_length = 100, null = True)                      ## 业务
##     project = models.CharField(max_length = 100, null = True)                        ## 项目
##     create_date = models.DateTimeField(default = timezone.now)
##     update_date = models.DateTimeField(default = timezone.now)
